package com.tang.net;


import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.tang.base.BaseResponse;
import com.tang.base.BaseView;
import com.tang.user.UserInfoManager;
import com.tang.util.GsonUtil;

import io.reactivex.functions.Consumer;

/**
 * Created by Snow on 2017/4/7.
 * Description:
 */

public abstract class SuccessConsumer<T> implements Consumer<T> {

    private final Context context;
    private BaseView mBaseView;

    public SuccessConsumer(BaseView baseView, Context context) {
        mBaseView = baseView;
        this.context = context;
    }

    @Override
    public void accept(T response) throws Exception {
        BaseResponse baseResponse = (BaseResponse) response;
        String code = baseResponse.getCode();
        if (code.equals(ResponseCode.SUCCESS)) {
            onSuccess(response);
        } else {
            switch (code) {
                case ResponseCode.TOKEN_EXPIRE:
                    if (!UserInfoManager.isLoginDisable) {
                        UserInfoManager.isLoginDisable = true;
                        UserInfoManager.getInstance().setToken("");
                        Intent intent = new Intent();
                        // 设置action, 隐式跳转
                        intent.setAction("top.hidoctor.auscultation.feature.activity.login.LoginActivity");
                        intent.addCategory(Intent.CATEGORY_DEFAULT);
                        context.startActivity(intent);
                    }
                    break;
            }
            mBaseView.onError(code, baseResponse.getMessage());
        }
    }

    public abstract void onSuccess(T response);
}
